using Domain;
using Domain.Services;
using Domain.Entities;
using Domain.Entities.Enums;
using Domain.Permissions;
using System;
using System.Web.UI.WebControls;
using UserInterface.Misc;
using System.Configuration;

namespace UserInterface
{
    public partial class AddTopic : BoardPageBase
    {
        private Forum forum;

        protected void Page_Load(object sender, EventArgs e)
        {
            short forumId = Convert.ToInt16(Request.QueryString["forumId"]);
            string pageTitle = (string)GetLocalResourceObject("Title");

            forum = ForumService.GetById(forumId);
            ForumPermissionManager forumPermissionManager = PermissionManager.CreateForumPermissionManager(forum);

            if (!forumPermissionManager.CreateTopic)
            {
                DieMessage(DieMessageType.InsufficientPermissions, "InsufficientPermissions");
            }

            MasterPage.TitleGenerator.PageTitle = pageTitle;
            MasterPage.HeaderText = pageTitle;
            MasterPage.AddPathItem(forum.Name, "ViewForum.aspx?forumId=" + forum.Id);
            MasterPage.AddPathItem(pageTitle, "AddTopic.aspx?forumId=" + forum.Id);

            if (!Page.IsPostBack)
            {
                TopicTypeRadioButtonList.Items.Add(new ListItem("Normal", Convert.ToString((byte)TopicType.Normal)));
                if (forumPermissionManager.CreateSticky)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Sticky", Convert.ToString((byte)TopicType.Sticky)));
                }
                if (forumPermissionManager.CreateAnnouncement)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Announcement", Convert.ToString((byte)TopicType.Announcement)));
                }
                if (forumPermissionManager.CreateFaq)
                {
                    TopicTypeRadioButtonList.Items.Add(new ListItem("Faq", Convert.ToString((byte)TopicType.Faq)));
                }

                TopicTypeRadioButtonList.SelectedIndex = 0;

                PreviewMessage.Visible = false;
            }
        }

        protected void AddButton_Click(object sender, EventArgs e)
        {
            string subject = SubjectTextBox.Text.Trim();
            string text = Editor.Text.Trim();

            if (subject != "" && text != "")
            {
                User user = CurrentSession.User;
                TopicType type = (TopicType)Convert.ToByte(TopicTypeRadioButtonList.SelectedValue);
                Topic topic = TopicService.NewTopic(forum, user, subject, text, type);

                string filePathFormat =
                    ConfigurationManager.AppSettings["AttachmentsDirectory"]
                    + "/"
                    + ConfigurationManager.AppSettings["AttachmentFileNameFormat"];
                MessageService.AddAttachments(
                    topic.StartMessage,
                    Attachments.AttachmentsWithFiles,
                    filePathFormat
                );

                Response.Redirect("ViewTopic.aspx?topicId=" + topic.Id);
            }
            else
            {
                MasterPage.Messages.AddCritical(Resources.Resource.InvalidFormData);
            }
        }

        protected void PreviewButton_Click(object sender, EventArgs e)
        {
            //TODO
            //Message message = new Message();
            //message.Poster = CurrentSession.User;
            //message.PostDateTime = DateTime.Now;
            //message.Text = Editor.Text;
            //message.Attachments = Attachments.AttachmentsWithFiles;
            //PreviewMessage.Visible = true;
            //PreviewMessage.Message = message;
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewForum.aspx?forumId=" + forum.Id);
        }
    }
}
